package com.group3.group3.user.control;

import com.fasterxml.jackson.databind.deser.std.UUIDDeserializer;
import com.group3.group3.common.control.BaseController;
import com.group3.group3.mybatis.entity.PtUserEntity;
import com.group3.group3.mybatis.flexigrid.PadingRstType;
import com.group3.group3.mybatis.flexigrid.PagingBean;
import com.group3.group3.mybatis.ztree.ZtreeEntity;
import com.group3.group3.user.entity.UserSearchEnity;
import com.group3.group3.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.MessageSource;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    private Logger logger =  LogManager.getLogger(this.getClass().getName());

    @Resource
    private UserService userService;
    @Resource
    private MessageSource messageSource;
    @Secured("ROLE_gqgy_data_user")
//    @Secured("ROLE_ADMIN")
    @RequestMapping("/userPage")
    public String userPage(){
        return "user/userPage";
    }



    @RequestMapping("/getUserInfolistByPage")
    @ResponseBody
    public PadingRstType<PtUserEntity> getUserInfolistByPage(UserSearchEnity search,PagingBean paging){
        logger.info(search);
        PadingRstType<PtUserEntity> ptUserEntityPadingRstType = userService.getUserInfolistByPage(search,paging);
        return ptUserEntityPadingRstType;
    }
    @RequestMapping("/deleteUserById")
    @ResponseBody
    public String deleteUserById(@RequestParam("userUuid") String userUuid){
        logger.info("userUuid:" + userUuid);
        userService.deleteUserById(userUuid);
        return getSuccessMessag("user.delete.success");
    }

    @RequestMapping("/getRoleZtreeListByUserId")
    @ResponseBody
    public List<ZtreeEntity> getRoleZtreeListByUserId(@RequestParam("userUuid") String userUuid){
        logger.info("userUuid:" + userUuid);
        List<ZtreeEntity> list = userService.getRoleZtreeListByUserId(userUuid);
        return list;
    }
    @RequestMapping("/saveRoleByUserId")
    @ResponseBody
    public String saveRoleByUserId(@RequestParam("userUuid") String userUuid,@RequestParam("roleUuids") String roleUuids){
        logger.info("userUuid:" + userUuid + " roleUuids:" + roleUuids);
        userService.saveRoleByUserId(userUuid,roleUuids);
        return getSuccessMessag("user.allocat.role.success");
    }
    @RequestMapping("/addUserById")
    @ResponseBody
    public String  addUserById(@RequestParam("niceName") String userNick,@RequestParam("userName") String userName,@RequestParam("password") String password,@RequestParam("mobile") String mobile,@RequestParam("email") String email){
        String uuid = UUID.randomUUID().toString();
        userService.addUser(uuid,userNick,userName,password,mobile,email);
        return getSuccessMessag("user.add.user.success");
    }
    @RequestMapping("/updateUser")
    @ResponseBody
    public String  updateUser(@RequestParam("userUuid")String uuid,@RequestParam("niceName") String userNick,@RequestParam("userName") String userName,@RequestParam("password") String password,@RequestParam("mobile") String mobile,@RequestParam("email") String email){
        userService.updateUser(uuid,userNick,userName,password,mobile,email);
        return getSuccessMessag("user.update.user.success");
    }


}
